{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 机器学习-K近邻\n",
    "### Airbnb 房价预测任务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"1.png\" style=\"width:800px;height:480px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3723, 8)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_listing=pd.read_csv(\"listings.csv\")\n",
    "#截取相关字段\n",
    "features=['accommodates','bedrooms','bathrooms','beds','price','minimum_nights','maximum_nights','number_of_reviews']\n",
    "df_listing=df_listing[features]\n",
    "df_listing.head()\n",
    "df_listing.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据特征：\n",
    "\n",
    "* accommodates: 可以容纳的旅客\n",
    "* bedrooms: 卧室的数量\n",
    "* bathrooms: 厕所的数量\n",
    "* beds: 床的数量\n",
    "* price: 每晚的费用\n",
    "* minimum_nights: 客人最少租了几天\n",
    "* maximum_nights: 客人最多租了几天\n",
    "* number_of_reviews: 评论的数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 解决问题\n",
    " ### 如果我有一个3个房间的房子，我能租多少钱呢？\n",
    "讲道理，咱是不是得去看看3个房间的别人都租到多少钱啊！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"2.png\" style=\"width:600px;height:230px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "K代表我们的候选对象个数，也就是找和我房间数量最相近的其他房子的价格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## K近邻原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"4.png\" style=\"width:600px;height:330px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "再综合考虑这三个我就得到了我的房子大概能值多钱啦！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 距离的定义\n",
    "如何才能知道哪些数据样本跟我最相近呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"6.png\" style=\"width:400px;height:80px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其中Qi到Qn是一条数据的所有特征信息，P1到Pn是另一条数据的所有特征信息【测试关联关系】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#如何操作进行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      461\n",
       "1     2294\n",
       "2      503\n",
       "3      279\n",
       "4       35\n",
       "5       73\n",
       "6       17\n",
       "7       22\n",
       "8        7\n",
       "9       12\n",
       "10       2\n",
       "11       4\n",
       "12       6\n",
       "13       8\n",
       "Name: distance, dtype: int64"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "our_acc_value=3\n",
    "df_listing[\"distance\"]=np.abs(df_listing['accommodates']-our_acc_value)\n",
    "df_listing['distance'].value_counts().sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们只有了绝对值来计算，和我们距离为0的（同样数量的房间）有461个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sample操作可以得到洗牌后的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "      <th>distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>$160.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>$350.00</td>\n",
       "      <td>2</td>\n",
       "      <td>30</td>\n",
       "      <td>65</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$50.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$95.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$50.00</td>\n",
       "      <td>7</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   accommodates  bedrooms  bathrooms  beds    price  minimum_nights  \\\n",
       "0             4       1.0        1.0   2.0  $160.00               1   \n",
       "1             6       3.0        3.0   3.0  $350.00               2   \n",
       "2             1       1.0        2.0   1.0   $50.00               2   \n",
       "3             2       1.0        1.0   1.0   $95.00               1   \n",
       "4             4       1.0        1.0   1.0   $50.00               7   \n",
       "\n",
       "   maximum_nights  number_of_reviews  distance  \n",
       "0            1125                  0         1  \n",
       "1              30                 65         3  \n",
       "2            1125                  1         2  \n",
       "3            1125                  0         1  \n",
       "4            1125                  0         1  "
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_listing.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#help(df_listing.sample)\n",
    "df_listing=df_listing.sample(frac=1,random_state=0) #frac:代表抽取比例为100% ，random_state:定义seed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "      <th>distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2478</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>102.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2830</th>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>65.00</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2680</th>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>125.00</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2646</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>400.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1716</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>115.00</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      accommodates  bedrooms  bathrooms  beds   price  minimum_nights  \\\n",
       "2478             3       1.0        1.0   1.0  102.00               2   \n",
       "2830             3       0.0        1.0   1.0   65.00               2   \n",
       "2680             3       2.0        1.0   2.0  125.00               1   \n",
       "2646             3       1.0        1.0   1.0  400.00               1   \n",
       "1716             3       1.0        1.0   1.0  115.00               2   \n",
       "\n",
       "      maximum_nights  number_of_reviews  distance  \n",
       "2478            1125                  3         0  \n",
       "2830               7                  1         0  \n",
       "2680              61                 24         0  \n",
       "2646            1125                  0         0  \n",
       "1716              10                 19         0  "
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_listing=df_listing.sort_values(\"distance\")\n",
    "#将price里面的数据全部float化\n",
    "df_listing[\"price\"]=df_listing[\"price\"].str.replace(\"\\$|,\",\"\")\n",
    "df_listing.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "如果我有一个3个房间的房子，我能租161.4钱呢？\n"
     ]
    }
   ],
   "source": [
    "price_value=df_listing.price.iloc[:5]\n",
    "mean_value=price_value.astype(float).mean()\n",
    "print(\"如果我有一个3个房间的房子，我能租{}钱呢？\".format(mean_value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#从上面我们可以看出来这种预测其实很有问题，样本延续欣很差，大量在均值区间过大，导致KNN不能够精确越策"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "得到了平均价格，也就是我们的房子大致的价格了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型的评估"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"7.png\" style=\"width:600px;height:250px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先制定好训练集和测试集【我们把数据集分为：train_df[训练集(75%)],test_df[测试集(25%)],test集其实查看模型拟合度】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#删除这个距离\n",
    "#df_listing=df_listing.drop(\"distance\",axis=1)\n",
    "train_df=df_listing.copy().iloc[:2792]\n",
    "test_df=df_listing.copy().iloc[2792:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#利用apply 对train_df进行操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 基于单变量预测模型####\n",
    " * 这个模型写法很重要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict_price(new_list_value,feature_column):\n",
    "    temp_df=train_df;\n",
    "    temp_df['distance']=np.abs(df_listing[feature_column]-new_list_value)\n",
    "    temp_df=temp_df.sort_values('distance')\n",
    "    knn5=temp_df.price.iloc[:5].astype(float)\n",
    "    price_mean=knn5.mean()\n",
    "    return price_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_df['predict_price']=test_df.accommodates.apply(predict_price,feature_column=\"accommodates\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "      <th>distance</th>\n",
       "      <th>predict_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2535</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>275.00</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1636</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>70.00</td>\n",
       "      <td>6</td>\n",
       "      <td>150</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>379</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>56.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1929</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>49.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>132.00</td>\n",
       "      <td>5</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1680</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>72.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>43</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3280</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>80.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>67</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3558</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>90.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1354</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>65.00</td>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3634</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>37</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>595</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>97.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>563</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>62.00</td>\n",
       "      <td>14</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1474</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>59.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3058</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>145.00</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1458</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>114.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>21</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2897</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>119.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2218</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>149.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2536</th>\n",
       "      <td>5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>290.00</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>704</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1300.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>418</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>70.00</td>\n",
       "      <td>7</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>55.00</td>\n",
       "      <td>20</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>250.00</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1762</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>45.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2720</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>65.00</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1859</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>125.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.00</td>\n",
       "      <td>1</td>\n",
       "      <td>365</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1408</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>32.00</td>\n",
       "      <td>5</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2909</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>45.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3364</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>75.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>26</td>\n",
       "      <td>2</td>\n",
       "      <td>71.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1879</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1825</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3272</th>\n",
       "      <td>12</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>529</th>\n",
       "      <td>12</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>600.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1656</th>\n",
       "      <td>12</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>6.0</td>\n",
       "      <td>283.00</td>\n",
       "      <td>4</td>\n",
       "      <td>90</td>\n",
       "      <td>26</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1885</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>99.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1825</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1878</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.00</td>\n",
       "      <td>1</td>\n",
       "      <td>365</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1875</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.00</td>\n",
       "      <td>1</td>\n",
       "      <td>365</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1326</th>\n",
       "      <td>12</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>375.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3305</th>\n",
       "      <td>12</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2000.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>808</th>\n",
       "      <td>12</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>215.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1825</td>\n",
       "      <td>34</td>\n",
       "      <td>9</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3524</th>\n",
       "      <td>13</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>295.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>571</th>\n",
       "      <td>13</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>720.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>562</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1403</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>685</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>399.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1658</th>\n",
       "      <td>14</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>283.00</td>\n",
       "      <td>4</td>\n",
       "      <td>90</td>\n",
       "      <td>19</td>\n",
       "      <td>11</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2106</th>\n",
       "      <td>15</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>12.0</td>\n",
       "      <td>499.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2757</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>245.00</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1850</th>\n",
       "      <td>15</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>180.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>26</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1806</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>330.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>6</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>657</th>\n",
       "      <td>15</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>500.00</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "      <td>41</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2688</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>10.0</td>\n",
       "      <td>749.00</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>611</th>\n",
       "      <td>16</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1250.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1224</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>499.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1596</th>\n",
       "      <td>16</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>299.00</td>\n",
       "      <td>3</td>\n",
       "      <td>365</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>763</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1594</th>\n",
       "      <td>16</td>\n",
       "      <td>10.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1250.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1402</th>\n",
       "      <td>16</td>\n",
       "      <td>8.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1200.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2560</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>60.00</td>\n",
       "      <td>3</td>\n",
       "      <td>60</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1818</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>16.0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>176.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>931 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      accommodates  bedrooms  bathrooms  beds    price  minimum_nights  \\\n",
       "2535             1       1.0        1.0   1.0   275.00               2   \n",
       "1636             1       1.0        1.0   1.0    70.00               6   \n",
       "379              1       1.0        1.0   1.0    56.00               1   \n",
       "1929             1       1.0        1.0   1.0    49.00               1   \n",
       "971              1       1.0        1.0   1.0   132.00               5   \n",
       "1680             1       1.0        1.0   1.0    72.00               1   \n",
       "3280             5       0.0        1.0   3.0    80.00               1   \n",
       "3558             1       1.0        1.0   1.0    90.00               1   \n",
       "1354             1       1.0        1.5   1.0    65.00               2   \n",
       "3634             1       1.0        1.5   1.0   100.00               3   \n",
       "595              1       1.0        1.0   1.0    97.00               1   \n",
       "563              1       0.0        1.0   1.0    62.00              14   \n",
       "1474             1       1.0        1.5   1.0    59.00               1   \n",
       "3058             5       2.0        1.0   3.0   145.00               3   \n",
       "1458             5       1.0        1.5   2.0   114.00               1   \n",
       "2897             5       2.0        1.0   3.0   119.00               3   \n",
       "2218             5       2.0        1.0   3.0   149.00               3   \n",
       "2536             5       3.0        2.5   3.0   290.00               3   \n",
       "704              1       1.0        1.0   1.0  1300.00               1   \n",
       "418              1       1.0        1.0   1.0    70.00               7   \n",
       "116              1       1.0        1.0   1.0    55.00              20   \n",
       "568              5       2.0        1.0   2.0   250.00               4   \n",
       "252              1       1.0        1.0   1.0   135.00               1   \n",
       "1762             1       1.0        1.0   1.0    45.00               1   \n",
       "2720             1       1.0        1.0   1.0    65.00               1   \n",
       "1859             1       1.0        1.0   1.0   125.00               2   \n",
       "410              1       1.0        1.0   1.0    50.00               1   \n",
       "1408             1       1.0        2.0   1.0    32.00               5   \n",
       "2909             1       1.0        0.5   1.0    45.00               1   \n",
       "3364             1       1.0        1.0   1.0    75.00               1   \n",
       "...            ...       ...        ...   ...      ...             ...   \n",
       "1879            12       1.0        NaN   1.0    99.00               1   \n",
       "3272            12       4.0        3.5   7.0   599.00               1   \n",
       "529             12       5.0        3.0   6.0   600.00               1   \n",
       "1656            12       4.0        3.5   6.0   283.00               4   \n",
       "1885            12       1.0        1.0   8.0    99.00               1   \n",
       "1878            12       1.0        NaN   1.0    99.00               1   \n",
       "1875            12       1.0        NaN   1.0    99.00               1   \n",
       "1326            12       3.0        2.5   4.0   375.00               1   \n",
       "3305            12       6.0        6.0   6.0  2000.00               1   \n",
       "808             12       5.0        2.0   5.0   215.00               2   \n",
       "3524            13       5.0        4.5   5.0   295.00               3   \n",
       "571             13       6.0        3.0   8.0   720.00               1   \n",
       "562             14       5.0        3.0   7.0   599.00               1   \n",
       "1403            14       5.0        2.0   7.0   599.00               1   \n",
       "685             14       5.0        3.0   7.0   399.00               1   \n",
       "1658            14       4.0        3.5   7.0   283.00               4   \n",
       "2106            15       6.0        4.5  12.0   499.00               2   \n",
       "2757            15       5.0        4.0   9.0   245.00               3   \n",
       "1850            15       3.0        2.0  10.0   180.00               1   \n",
       "1806            15       5.0        3.0   9.0   330.00               1   \n",
       "657             15       6.0        3.0   9.0   500.00               1   \n",
       "2688            15       5.0        3.5  10.0   749.00               3   \n",
       "611             16       8.0        8.0  16.0  1250.00               1   \n",
       "1224            16       1.0        2.0   1.0   499.00               1   \n",
       "1596            16       5.0        3.5   5.0   299.00               3   \n",
       "763             16       1.0        1.0   1.0  1000.00               1   \n",
       "1594            16      10.0        8.0  13.0  1250.00               3   \n",
       "1402            16       8.0        6.0  13.0  1200.00               3   \n",
       "2560            16       1.0        1.0   2.0    60.00               3   \n",
       "1818            16       1.0        0.5  16.0    10.00               1   \n",
       "\n",
       "      maximum_nights  number_of_reviews  distance  predict_price  \n",
       "2535               2                  0         2           71.8  \n",
       "1636             150                 10         2           71.8  \n",
       "379             1125                  0         2           71.8  \n",
       "1929            1125                  0         2           71.8  \n",
       "971             1125                  0         2           71.8  \n",
       "1680            1125                 43         2           71.8  \n",
       "3280            1125                 67         2          176.8  \n",
       "3558            1125                  3         2           71.8  \n",
       "1354              45                  3         2           71.8  \n",
       "3634            1125                 37         2           71.8  \n",
       "595             1125                 15         2           71.8  \n",
       "563             1125                  1         2           71.8  \n",
       "1474            1125                  0         2           71.8  \n",
       "3058              30                  0         2          176.8  \n",
       "1458            1125                 21         2          176.8  \n",
       "2897            1125                 10         2          176.8  \n",
       "2218            1125                  6         2          176.8  \n",
       "2536              30                 31         2          176.8  \n",
       "704             1125                  0         2           71.8  \n",
       "418             1125                  0         2           71.8  \n",
       "116               21                  0         2           71.8  \n",
       "568               15                  2         2          176.8  \n",
       "252             1125                  0         2           71.8  \n",
       "1762            1125                  0         2           71.8  \n",
       "2720              10                  3         2           71.8  \n",
       "1859            1125                  0         2           71.8  \n",
       "410              365                  2         2           71.8  \n",
       "1408            1125                  1         2           71.8  \n",
       "2909            1125                  3         2           71.8  \n",
       "3364            1125                 26         2           71.8  \n",
       "...              ...                ...       ...            ...  \n",
       "1879            1825                  0         9          176.8  \n",
       "3272            1125                  0         9          176.8  \n",
       "529             1125                  0         9          176.8  \n",
       "1656              90                 26         9          176.8  \n",
       "1885            1825                  1         9          176.8  \n",
       "1878             365                  0         9          176.8  \n",
       "1875             365                  1         9          176.8  \n",
       "1326            1125                  4         9          176.8  \n",
       "3305            1125                  0         9          176.8  \n",
       "808             1825                 34         9          176.8  \n",
       "3524            1125                  1        10          176.8  \n",
       "571             1125                  1        10          176.8  \n",
       "562             1125                  0        11          176.8  \n",
       "1403            1125                  0        11          176.8  \n",
       "685             1125                  2        11          176.8  \n",
       "1658              90                 19        11          176.8  \n",
       "2106            1125                  0        12          176.8  \n",
       "2757              30                  0        12          176.8  \n",
       "1850            1125                 26        12          176.8  \n",
       "1806            1125                  6        12          176.8  \n",
       "657               28                 41        12          176.8  \n",
       "2688              30                  0        12          176.8  \n",
       "611             1125                  0        13          176.8  \n",
       "1224            1125                  0        13          176.8  \n",
       "1596             365                  8        13          176.8  \n",
       "763             1125                  0        13          176.8  \n",
       "1594            1125                  0        13          176.8  \n",
       "1402            1125                 10        13          176.8  \n",
       "2560              60                  0        13          176.8  \n",
       "1818               2                  0        13          176.8  \n",
       "\n",
       "[931 rows x 10 columns]"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df.head(10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样我们就得到了测试集中，所以房子的价格了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个模型好不好，我们需要用均方根误差来检验模型好还是不好，如果均方差误差越少说明模型拟合度就越好"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### root mean squared error (RMSE)均方根误差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"8.png\" style=\"width:700px;height:100px;float:left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_df['squared_error'] = (test_df['predict_price']-test_df['price'].astype(float))**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mean=test_df['squared_error'].mean()\n",
    "RMSE=mean**(1/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "225.01216207731247"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RMSE #说明均方根大，拟合度不好"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 不同的变量效果会不会不同呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accommodates预测结果是：225.01216207731247\n",
      "bedrooms预测结果是：209.6831300739141\n",
      "bathrooms预测结果是：218.44816171241078\n",
      "number_of_reviews预测结果是：244.5158980595471\n"
     ]
    }
   ],
   "source": [
    "for feature in ['accommodates','bedrooms','bathrooms','number_of_reviews']:\n",
    "    test_df['predict_price']=test_df.accommodates.apply(predict_price,feature_column=feature)\n",
    "    test_df['squared_error'] = (test_df['predict_price']-test_df['price'].astype(float))**2\n",
    "    mean=test_df['squared_error'].mean()\n",
    "    RMSE=mean**(1/2)\n",
    "    print('{}预测结果是：{}'.format(feature,RMSE))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看起来结果差异还是蛮大的，接下来我们要做的就是综合利用所有的信息来一起进行测试"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 由于不同特征数据出现导致RMSE结果浮动有点大，所以我们拿到数据首先进行标准化处理【特征标准化】：特征标准化+特征归一化；\n",
    "#### sklearn【http://scikit-learn.org/stable/】很重要机器学习库，6大库都很重要：分类，回归，聚类，降维，模块选择，标准化 可以随时看看。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<img src=\"特征数据标准化.png\" style=\"width:800px;height:480px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数据预处理请查看数据特征标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3723, 8)\n",
      "(3671, 8)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.401420</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>0.297386</td>\n",
       "      <td>0.081119</td>\n",
       "      <td>-0.341421</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.399466</td>\n",
       "      <td>2.129508</td>\n",
       "      <td>2.969551</td>\n",
       "      <td>1.141704</td>\n",
       "      <td>1.462622</td>\n",
       "      <td>-0.065047</td>\n",
       "      <td>-0.016606</td>\n",
       "      <td>1.706767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.095648</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>1.265170</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.718699</td>\n",
       "      <td>-0.065047</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.482571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.596625</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.391501</td>\n",
       "      <td>-0.341421</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.401420</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.718699</td>\n",
       "      <td>1.316824</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   accommodates  bedrooms  bathrooms      beds     price  minimum_nights  \\\n",
       "0      0.401420 -0.249501  -0.439211  0.297386  0.081119       -0.341421   \n",
       "1      1.399466  2.129508   2.969551  1.141704  1.462622       -0.065047   \n",
       "2     -1.095648 -0.249501   1.265170 -0.546933 -0.718699       -0.065047   \n",
       "3     -0.596625 -0.249501  -0.439211 -0.546933 -0.391501       -0.341421   \n",
       "4      0.401420 -0.249501  -0.439211 -0.546933 -0.718699        1.316824   \n",
       "\n",
       "   maximum_nights  number_of_reviews  \n",
       "0       -0.016575          -0.516779  \n",
       "1       -0.016606           1.706767  \n",
       "2       -0.016575          -0.482571  \n",
       "3       -0.016575          -0.516779  \n",
       "4       -0.016575          -0.516779  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "from sklearn.preprocessing import  StandardScaler\n",
    "\n",
    "features = ['accommodates','bedrooms','bathrooms','beds','price','minimum_nights','maximum_nights','number_of_reviews']\n",
    "dc_listings = pd.read_csv('listings.csv')\n",
    "dc_listings = dc_listings[features]\n",
    "#dc_listings.head()\n",
    "#处理price处理\n",
    "dc_listings[\"price\"]=dc_listings.price.str.replace(\"\\$|,\",'').astype(float)\n",
    "print(dc_listings.shape)\n",
    "#将去除控制\n",
    "dc_listings=dc_listings.dropna()\n",
    "print(dc_listings.shape)\n",
    "#进行z-score normalization\n",
    "dc_listings[features] = StandardScaler().fit_transform(dc_listings[features])\n",
    "normalized_listings=dc_listings[features]\n",
    "normalized_listings.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 分训练库和验证库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "norm_train_df=normalized_listings.copy().iloc[:2792]\n",
    "normal_test_df=normalized_listings.copy().iloc[2792:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"9.png\" style=\"width:700px;height:400px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 多变量距离的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### scipy中已经有现成的距离的计算工具了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#用scipy来计算多个变量之前D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.8698195887683675"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.spatial import distance\n",
    "first_listing=normalized_listings.iloc[0][['accommodates','bedrooms']]\n",
    "five_listing=normalized_listings.iloc[20][['accommodates','bedrooms']]\n",
    "first_fifth_distance=distance.euclidean(first_listing,five_listing)\n",
    "first_fifth_distance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 多变量KNN模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict_price_multivariate(new_listing_value,feature_columns):\n",
    "    temp_df = norm_train_df\n",
    "    temp_df['distance'] = distance.cdist(temp_df[feature_columns],[new_listing_value[feature_columns]])\n",
    "    temp_df = temp_df.sort_values('distance')\n",
    "    knn_5 = temp_df.price.iloc[:5]\n",
    "    predicted_price = knn_5.mean()\n",
    "    return(predicted_price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7894063922577537"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = ['accommodates', 'bathrooms']\n",
    "normal_test_df['predicted_price'] = normal_test_df[cols].apply(predict_price_multivariate,feature_columns=cols,axis=1)\n",
    "#进行评估计算\n",
    "normal_test_df['squared_error']=(normal_test_df[\"predicted_price\"]-normal_test_df[\"price\"])**(2)\n",
    "mse = normal_test_df['squared_error'].mean()\n",
    "RMSE=mse**(1/2)\n",
    "RMSE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sklearn完成KNN算法【更加简单】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "#结果预测\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "cols = ['accommodates','bedrooms']\n",
    "#实例化\n",
    "knn = KNeighborsRegressor()\n",
    "#构建多变量KNN模型\n",
    "knn.fit(norm_train_df[cols], norm_train_df['price'])\n",
    "#测试集进行模型测试[预测结果]\n",
    "two_features_predictions = knn.predict(normal_test_df[cols])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#评测拟合度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.842682470482\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error\n",
    "#求均方差\n",
    "two_features_mse = mean_squared_error(normal_test_df['price'], two_features_predictions)\n",
    "#求均方根\n",
    "two_features_rmse = two_features_mse ** (1/2)\n",
    "print(two_features_rmse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sklearn结果影响因素分析拟合度调节分两个因素\n",
    "* n_neighbors大小会影响到结果\n",
    "* use_cols 大小也会影响到结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.809751669757\n"
     ]
    }
   ],
   "source": [
    "#结果预测\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "cols = ['accommodates','bedrooms']\n",
    "#实例化\n",
    "knn = KNeighborsRegressor(n_neighbors=44)\n",
    "#构建多变量KNN模型\n",
    "knn.fit(norm_train_df[cols], norm_train_df['price'])\n",
    "#测试集进行模型测试[预测结果]\n",
    "two_features_predictions = knn.predict(normal_test_df[cols])\n",
    "from sklearn.metrics import mean_squared_error\n",
    "#求均方差\n",
    "two_features_mse = mean_squared_error(normal_test_df['price'], two_features_predictions)\n",
    "#求均方根\n",
    "two_features_rmse = two_features_mse ** (1/2)\n",
    "print(two_features_rmse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加入更多的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.824383853088\n"
     ]
    }
   ],
   "source": [
    "#结果预测\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "cols = ['accommodates','bedrooms','bathrooms','beds','minimum_nights','maximum_nights','number_of_reviews']\n",
    "#实例化\n",
    "knn = KNeighborsRegressor()\n",
    "#构建多变量KNN模型\n",
    "knn.fit(norm_train_df[cols], norm_train_df['price'])\n",
    "#测试集进行模型测试[预测结果]\n",
    "two_features_predictions = knn.predict(normal_test_df[cols])\n",
    "from sklearn.metrics import mean_squared_error\n",
    "#求均方差\n",
    "two_features_mse = mean_squared_error(normal_test_df['price'], two_features_predictions)\n",
    "#求均方根\n",
    "two_features_rmse = two_features_mse ** (1/2)\n",
    "print(two_features_rmse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## KNN用途主要特征属性分类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"KNN类别预测示意图.png\" style=\"width:800px;height:480px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## KNN算法最大缺点\n",
    "* 从KNN思想就是找欧氏距离，每找一次就需要遍历整个特征值，如果特征值很多，需要找的距离特征比较大，这样做法肯定有问题\n",
    "* 结论数据量很大的时候用KNN进行分类预测肯定是有问题的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
